package cn.hp.servlet;

import cn.hp.service.UserService;
import cn.hp.service.UserServiceImp;
import cn.hp.util.ResultJson;
import cn.hp.util.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp){
        String name = req.getParameter("name");
        String pwd = req.getParameter("pwd");
        UserService userService = new UserServiceImp();
        Map map = userService.login(name, pwd);
        if(map!=null){
            req.getSession().setAttribute("user", map)  ;
            ResultJson success = ResultJson.success(map);
            Utils.returnJson(success,resp);
        }else{
            ResultJson error = ResultJson.error();
            Utils.returnJson(error,resp);
        }
    }
}
